
package com.xbongbong.admin.model.impl;

import com.xbongbong.admin.domain.dao.RaidersCorpDao;
import com.xbongbong.admin.domain.entity.RaidersCorpEntity;
import com.xbongbong.admin.domain.entity.ext.RaidersCorpEntityExt;
import com.xbongbong.admin.model.RaidersCorpModel;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.constant.RaidersConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;


/**
* @author autoGen
* @time 2019-09-18 13:56:16
*/
@Service("raidersCorpModel")
public class RaidersCorpModelImpl implements RaidersCorpModel {

	@Resource
	private RaidersCorpDao dao;

	@Override
	public Integer insert(RaidersCorpEntity entity){
		long now = DateUtil.getInt();
		entity.setAddTime(now);
		entity.setUpdateTime(now);

		return dao.insert(entity);
	}

	@Override
	public Integer update(RaidersCorpEntity entity){
		long now = DateUtil.getInt();
		entity.setUpdateTime(now);

		return dao.update(entity);
	}

	@Override
	public Integer save(RaidersCorpEntity entity){

		if(entity.getId() == null || Objects.equals(0L, entity.getId())) {
			return insert(entity);
		}
		return update(entity);
	}

	@Override
	public void insertBatch(List<RaidersCorpEntity> entityList) {
    	dao.insertBatch(entityList);
    }

	@Override
	public Integer deleteByKey( Long key,  String corpid){
		return dao.deleteByKey(key , corpid);
	}

	@Override
	public RaidersCorpEntity getByKey( Long key,  String corpid){
		return dao.getByKey(key , corpid);
	}

	@Override
	public RaidersCorpEntity getByRaidersId( Long raidersId,  String corpid){
		return dao.getByRaidersId(raidersId , corpid);
	}

	@Override
	public List<RaidersCorpEntity> findEntitys(Map<String ,Object>  param){
		return dao.findEntitys(param);
	}

    @Override
	public Integer getEntitysCount(Map<String ,Object>  param){
		return dao.getEntitysCount(param);
	}

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
	public List<RaidersCorpEntityExt> getCorpCount(Map<String, Object> param) {
		return dao.getCorpCount(param);
	}

	@Override
	public List<RaidersCorpEntityExt> getUnLockCorpCount(Collection<Long> raidersIdIn) {
		return dao.getUnLockCorpCount(raidersIdIn);
	}

	@Override
	public List<RaidersCorpEntity> getRaidersCorpList(String corpid, Set<Long> raidersIdIn, boolean sortFlag) {
		if (raidersIdIn == null || raidersIdIn.size() == 0) {
		    return new ArrayList<>();
		}
		Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
		//此时会过滤公司id，只得到该公司的攻略
		map.put("corpid", corpid);
		map.put("del", 0);
		map.put("raidersIdIn", raidersIdIn);
		if (sortFlag) {
			//按照顺序获取私有攻略
			map.put("orderByStr", RaidersConstant.ORDER_BY_STR_LIST);
		}
		return findEntitys(map);
	}

	@Override
	public Map<Long, RaidersCorpEntity> corpRaidersMap(List<RaidersCorpEntity> list, boolean sortFlag) {
		if (list == null) {
		    return new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
		}
		//攻略id-攻略公司关系实体
		Map<Long, RaidersCorpEntity> corpRaidersMap;
		if (sortFlag) {
			//list已经按照sort值排序，封装的map也需要有序
		    corpRaidersMap = new LinkedHashMap<>();
		} else {
			corpRaidersMap = new HashMap<>(list.size());
		}
		for (RaidersCorpEntity entity : list) {
			corpRaidersMap.put(entity.getRaidersId(), entity);
		}
		return corpRaidersMap;
	}

	@Override
	public Integer getTotalScoreSum(Map<String, Object> param) {
		return dao.getTotalScoreSum(param);
	}

	@Override
	public Integer getRaidersCorpCount(Map<String, Object> param) {
		return dao.getRaidersCorpCount(param);
	}

	@Override
	public void updateBatch(List<RaidersCorpEntity> list) {
		dao.updateBatch(list);
	}

}

